const SERVICE = require("../../lib/js/service.js");

Page({
  onLoad(){
    
  },
  onShow(){
    this.manageBankCard();
  },

  /*分享*/
  onShareAppMessage(){
  },

  /**
   * @explain manageBankCard 管理银行卡
   * @param thirdCookie 用户标识
   * 
   * */ 
  manageBankCard(){
    wx.showNavigationBarLoading();
    let _this = this;
    SERVICE.POST(SERVICE.URL.manageBankCard,{
      thirdCookie: wx.getStorageSync("thirdCookie")
    },(status,res)=>{
      wx.hideNavigationBarLoading();
      if(res.data.status === "success"){
        console.log(res);
        _this.setData({
          list: res.data.list
        });
      }else{
        wx.showToast({
          title: "获取银行卡失败",
          icon: "none",
          mask: true
        })
      }

    });
  },

  getItem(e){
    console.log(e);
    let status = e.currentTarget.dataset.status;
    this.setData({
      status
    });
  },

  /**
   * @explain formSubmit 设置默认银行卡
   * @param card_id 银行卡ID
   * */ 
  formSubmit(e){
    let _this = this, td = this.data, id = e.detail.value.radio;
    if(!td.status){
      if (id){
       SERVICE.POST(SERVICE.URL.setDefaultBankCord, {
         thirdCookie: wx.getStorageSync("thirdCookie"),
         card_id: id
       }, (status, res) => {
         if (res.data.status === "success") {
           wx.showToast({
             title: "设置成功",
             icon: "none",
             mask: true,
             success: function () {
               _this.manageBankCard();
             }
           });
         } else {
           wx.showToast({
             title: "设置失败",
             icon: "none",
             mask: true
           });
         }
       });
     }else{
       wx.showToast({
         title: "请选择银行卡",
         icon: "none",
         mask: true
       });
     }
    }else{
      wx.showToast({
        title: "已经是默认的啦",
        icon: "none",
        mask: true
      })
    }
  },

  /**
   * @explain Delete 删除银行卡
   * @param thirdCookie 用户标识
   * @param card_id 卡号ID
   * 
   * */ 
  Delete(e){
   let _this = this, id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确认删除该银行卡吗",
      success: function(res){
        if(res.confirm){
          wx.showLoading({
            title: "删除中...",
            mask: true
          });
          SERVICE.POST(SERVICE.URL.deleteBankCard,{
            thirdCookie: wx.getStorageSync("thirdCookie"),
            card_id: id
          },(status,res)=>{
            wx.hideLoading();
            if(res.data.status === "success"){
              _this.manageBankCard();
            }else{
              wx.showToast({
                title: "删除失败",
                icon: "none",
                mask: true
              });
            }
          });
        }
      }
    })
  },
  add() {
    wx.navigateTo({
      url: "../add-bank-card/add-bank-card",
    })
  }
});